var t = getApp(),
  t1 = getApp(),
  a = t.requirejs("core"),
  i = t.requirejs("biz/diypage"),
  e = t.requirejs("jquery");
var QQMapWX = t.requirejs('qqmap-wx-jssdk');
Page({
  data: {
    merchid: t.globalData.mid,
    merch: [],
    category:[],
    banners:[],
    cateid: 0,
    page: 1,
    storeInfo:[],
    isnew:0,
    isrecommand:1,
    loading: false,
    loaded: false,
    list: [],
    approot: t.globalData.approot,

    imgUrls: [
      'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg',
      'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg',
      'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg'
    ],
    autoplay: false,
    interval: 5000,
    duration: 600

  },
  
  onLoad: function (t) {
    var storeInfo =  wx.getStorageSync("storeInfo");
    if (storeInfo){
      this.setData({
        storeInfo: storeInfo
      })
    }
    t1.url(t), wx.getSystemInfo({
      success: function (e) {
      }
    }),wx.getSetting({
      success:function(res)
      {
        console.log(res);
      }
    }), i.get(this, "member", function (e) { }), "" == t1.getCache("userinfo") && wx.redirectTo({
      url: "/pages/message/auth/index"
    });
    if(t.id) {this.setData({
      merchid: t.id
    })};
      this.getMerch();
      this.getList();
      var  cityInfo = wx.getStorageSync("cityInfo");
      if (!cityInfo){
        this.dwreLoad();
      }
   
  },
  getMerch: function () {
    var t = this;
    var cityInfo = wx.getStorageSync("cityInfo");
    a.get("changce/merchStore/get_detail", { id: t.data.merchid, cityInfo: cityInfo}, function (a) {
      t.setData({
        banners: a.merch.banners
      })
      if (t.data.storeInfo=='') {
        t.setData({
          merch: a.merch,
         
          storeInfo: a.merch.storeList[0]
        });
        wx.setStorageSync("storeInfo", a.merch.storeList[0]);
     
      }else{
        t.setData({
          merch: a.merch,
        })
      }
     
    });
    a.get("changce/merchStore/get_cateList", { id: t.data.merchid }, function (a) {
      t.setData({
        category: a.category
      })
    })
  },
  getList: function () {
    var t = this;
    
    //console.log(this.data.merchid);
    a.loading(),
      this.setData({
        loading: true
      }),
      a.get("changce/merchStore/goods_list", {
        page: this.data.page,
        cate: this.data.cateid,
        id: t.data.merchid,
        isnew: this.data.isnew,
        isrecommand: this.data.isrecommand,
      }, function (e) {
        var i = {
          loading: false,
          total: e.total,
          pagesize: e.pagesize
        };
        e.list.length > 0 && (i.page = t.data.page + 1, i.list = t.data.list.concat(e.list), e.list.length < e.pagesize && (i.loaded = true), t.setSpeed(e.list)),
          t.setData(i),
          a.hideLoading()
      })
  },
  modify_shop:function(){
    console.log('need code');
    wx.navigateTo({
      url: 'adress',
    })
  },
  clickrec: function () {
    this.setData({
      isrecommand: 1,
      cateid: 0,
      isnew: 0,
      page:1,
      loading: false,
      loaded: false,
      list: []
    })
    this.getList()
  },
  catebtn: function (t) {

    this.setData({
      isrecommand: 0,
      isnew: 1,
      cateid: t.currentTarget.dataset.cateid,
      page: 1,
      loading: false,
      loaded: false,
      list: []
    })
    this.getList()
  },
  clicknew: function () {
    this.setData({
      isrecommand: 0,
      isnew: 1,
      cateid: 0,
      page: 1,
      loading: false,
      loaded: false,
      list: []
    })
    this.getList()
  },
  clickall: function () {
    this.setData({
      isrecommand: 0,
      isnew: 0,
      cateid: 0,
      page: 1,
      loading: false,
      loaded: false,
      list: []
    })
    this.getList()
  },
  setSpeed: function (t) {
    if (t && !(t.length < 1))
      for (var a in t) {
        var e = t[a];
        if (!isNaN(e.lastratio)) {
          var i = e.lastratio / 100 * 2.5,
            s = wx.createContext();
          s.beginPath(),
            s.arc(34, 35, 30, .5 * Math.PI, 2.5 * Math.PI),
            s.setFillStyle("rgba(0,0,0,0)"),
            s.setStrokeStyle("rgba(0,0,0,0.2)"),
            s.setLineWidth(4),
            s.stroke(),
            s.beginPath(),
            s.arc(34, 35, 30, .5 * Math.PI, i * Math.PI),
            s.setFillStyle("rgba(0,0,0,0)"),
            s.setStrokeStyle("#ffffff"),
            s.setLineWidth(4),
            s.setLineCap("round"),
            s.stroke();
          var o = "coupon-" + e.id;
          wx.drawCanvas({
            canvasId: o,
            actions: s.getActions()
          })
        }
      }
  },
  bindTab: function (t) {
    var e = a.pdata(t).cateid;
    this.setData({
      cateid: e,
      page: 1,
      list: []
    }),
      this.getList()
  },
  onReachBottom: function () {
    this.data.loaded || this.data.list.length == this.data.total || this.getList()
  },
  jump: function (t) {
    var e = a.pdata(t).id;
    e > 0 && wx.navigateTo({
      url: "/pages/sale/coupon/detail/index?id=" + e
    })
  },
  cartdec: function (e) {
    var a = this;
    var goods_id = e.currentTarget.dataset.gid;
    var list = a.data.list;
    for (var i = 0; i < list.length; i++) {
      if (goods_id == list[i].id) {
        if ((list[i].catCount - 1) >= 0) {
          list[i].catCount = list[i].catCount - 1;
     
          a.cartUpdate(list[i].catCount, goods_id, list);
          break;
        } else {
          wx.showToast({
            title: '数量不能为0'
          });
        }

      }
    }
  },
  cartadd: function (e) {
    var a = this;
    var goods_id = e.currentTarget.dataset.gid;
    var list = a.data.list;
    for (var i = 0; i < list.length; i++) {
      if (goods_id == list[i].id) {

        if ((list[i].catCount + 1) <= list[i].total) {
          list[i].catCount = parseInt(list[i].catCount) + parseInt(1);
          a.cartUpdate(list[i].catCount, goods_id, list);
          break;
        } else {
          wx.showToast({
            title: '库存不足了！'
          });
        }

      }
    }
  },
  cartUpdate: function (num, goods_id,list) {
    var t = this;
    a.loading(),
    a.post("changce/merchStore/cateUpdate", {total:num,id:goods_id}, function(i) {
          if(i.error==0){
            t.setData({
              list:list
            })
          }else{
            wx.showToast({
              title: i.message
            });
          }
         a.hideLoading()
          return 0;
    })
    

  },
  onSwiperTap:function(event){
      var url = event.target.dataset.url;
      if(url){
        wx.navigateTo({
          url: url,
        })
      }
     
  },
  dwreLoad: function () {
    var that = this
    console.log(t.globalData);
    let qqmapsdk = new QQMapWX({
      key: t.globalData.mapKey // 必填
    });
    //1、获取当前位置坐标
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        //2、根据坐标获取当前位置名称，显示在顶部:腾讯地图逆地址解析
        console.log(res);
        wx.setStorageSync("cityInfo", res);
        qqmapsdk.reverseGeocoder({
          location: {
            latitude: res.latitude,
            longitude: res.longitude
          },
          success: function (addressRes) {
            res['address'] = addressRes.result.address;
            wx.setStorageSync("cityInfo", res);
           // that.onLoad();
          }
        })
      },
      fail: function () {
        wx.getSetting({
          success: function (t) {
            0 == t.authSetting["scope.userLocation"] && wx.showModal({
              title: "提示",
              content: "您点击了拒绝授权,无法正常使用功能，点击确定重新获取授权。",
              showCancel: !1,
              success: function (t) {
                t.confirm && (wx.openSetting({
                  success: function (t) {
                    t.authSetting["scope.userLocation"];
                    that.dwreLoad();
                  }
                }));
              }
            });
          }
        });
      }
    })
  },
  imgurl:function(e){
    wx.navigateTo({
      url: e.currentTarget.dataset.url
    })
  }
})